﻿using System;
using System.Windows.Input;

namespace SearchForWorkitemsChanges
{
    /// <summary>
    /// Class implements <see cref="ICommand"/> that represent a generic <see cref="ICommand"/>. 
    /// On instance creation (by calling constructor), you have to push a lambda expression named <c>Execute</c> that will be execute on Command's call.
    /// You can also push another lambda expression called <c>CanExecute</c> that will be called when CanExecuteChanged's event is raised.
    /// To force raise of CanExecuteChanged, caller can use <see cref="NotifyCanExecuteChanged"/>.
    /// </summary>
    internal class RelayCommand : ICommand
    {
        private Action<object> execCmd;

        private Func<object, bool> canExecCmd;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand" /> class with an execute lambda expression.
        /// </summary>
        /// <param name="execute">The execute command</param>
        public RelayCommand(Action<object> execute)
        {
            this.execCmd = execute;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand" /> class with a canexecute and an execute lambda expressions.
        /// </summary>
        /// <param name="canExecute">The can execute lambda expression.</param>
        /// <param name="execute">The execute lambda expression.</param>
        public RelayCommand(Func<object, bool> canExecute, Action<object> execute)
            : this(execute)
        {
            this.canExecCmd = canExecute;
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            if(this.canExecCmd != null)
                return this.canExecCmd(parameter);
            return true;
        }

        /// <summary>
        /// Occurs when [can execute changed].
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Notifies the can execute changed.
        /// </summary>
        public void NotifyCanExecuteChanged()
        {
            if (this.CanExecuteChanged != null)
                App.Current.Dispatcher.Invoke(() => this.CanExecuteChanged(this, new EventArgs()));
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            if (this.execCmd != null)
                this.execCmd(parameter);
        }
    }
}
